/**
 * 
 */
package com.jie.eclipse.pan.data;

import java.sql.ResultSet;
import java.util.Vector;

import com.jie.eclipse.pan.utils.StringBuilderEx;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author yanjie
 * 数据对象集合
 * 2011/9/28 上午8:43:53
 */
public class BaseDataEntities  extends java.util.Vector<BaseDataEntity> {


	/**
	 * 序列标识
	 */
	private static final long serialVersionUID = -6002618898839162833L;

	
	/**
	 * 将数据对象集合导出为JSON数组
	 * @param bIncludeEmpty
	 * @return
	 */
	public JSONArray ToJSONArray(boolean bIncludeEmpty)
	{
		Vector<JSONObject> arr = new Vector<JSONObject>();
		for(BaseDataEntity dataEntity:this)
		{
			JSONObject jsonObject = new JSONObject();
			dataEntity.FillJSONObject(jsonObject, bIncludeEmpty);
			arr.add(jsonObject);
		}
		return JSONArray.fromArray(arr.toArray());
	}
	
	
	public String TOJSONString(){
		JSONArray json = null;
		try{
			json =  this.ToJSONArray(true);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(json == null)
			json = new JSONArray();
		
		return json.toString();
	}

	/* (non-Javadoc)
	 * @see java.util.Vector#clone()
	 */
	@Override
	public synchronized Object clone()
	{
		BaseDataEntities dataEntities = new BaseDataEntities();
		
		for(BaseDataEntity dataEntity:this)
		{
			BaseDataEntity temp = new BaseDataEntity();
			dataEntity.CopyTo(temp,false);
			dataEntities.add(temp);
		}
		return dataEntities;
	}
	
	
	
	public static BaseDataEntities FromJSONArray(JSONArray jsonArray)
	{
		BaseDataEntities dataEntities = new BaseDataEntities();
		
		for(int i=0;i<jsonArray.length();i++)
		{
			JSONObject jsonobject = jsonArray.getJSONObject(i);
			BaseDataEntity dataEntity = BaseDataEntity.FromJSONObject(jsonobject);
			dataEntities.add(dataEntity);
		}
		return dataEntities;
	}
	
	public static BaseDataEntities FromResultSet(ResultSet rs) throws Exception{
		BaseDataEntities rows = new BaseDataEntities();
		if(rs == null)
			return rows;
		
		while(rs.next()){
			BaseDataEntity row = new BaseDataEntity();
			row.FromResultSet(rs,true);
			rows.add(row);
		}
		return rows;
	}

	/**
	 * @param columnName
	 * @param strSpreate
	 * @param StrSplit
	 */
	public String ToColumnString(String columnName,String strSpreate,String strSplit){
		if(this == null || this.size() == 0)
			return "";
		StringBuilderEx sb = new StringBuilderEx();
		boolean bFirst = true ;
		for(BaseDataEntity dataEntity:this)
		{			
			if(bFirst)
			{
				sb.Append("%1$s%2$s%1$s",strSpreate,dataEntity.get(columnName));
			} 
			else 
			{
				sb.Append("%3$s%1$s%2$s%1$s",strSpreate,dataEntity.get(columnName),strSplit);
			}
			bFirst = false;
		}		
		return sb.toString();
	}
}
